package com.util;

import java.util.List;
import java.util.Random;

public class SmsUtil {
	public static void main(String[] args) {
//		boolean f = send("19977107527", "1234");
		boolean f = send("19973241392", "666666");
		System.out.println(f);
		int count = getMessageCount();
		System.out.println(count);
	}

	/**
	 * 随机生成验证码
	 * @param n		位数
	 * @return
	 */
	public static String createCode(int n) {
		String str = "0123456789";
		StringBuffer sb = new StringBuffer();
		Random random = new Random();
		for (int i = 0; i < n; i++) {
			int index = random.nextInt(10); // [0-9] 间随机数
			char ch = str.charAt(index);
			sb.append(ch + "");
		}
		return sb.toString();
	}

	private static String url = "http://sms.253.com/msg/send/";

	// YZM4840909
	// wbBu2i9sk
	public  static String account ="YZM4840909";// "N433130_N7036176"; // 发送短信的账号(非登录账号) (示例:N987654)
	private static String pswd ="wbBu2i9sk"; //"Cp7s4foW6k4244";// 发送短信的密码(非登录密码)

	// 可用条数/
	public static int getMessageCount() {
		String url = "http://sms.253.com/msg/balance";// 应用地址(无特殊情况时无需修改)
		try {
			String returnString = HttpSender.query(url,account, pswd);

			return Integer.parseInt(returnString.split(",")[1].replaceAll("\\s", ""));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}

	/**
	 * 发送国内短信验证码
	 * @param mobile
	 * @param content
	 * @return
	 */
	public static boolean send(String mobile, String content) {
		try {
			if (StringUtil.isNumber(content))
			{
				HttpSender.SendPost(url, account, pswd, "验证是"+content+", 5分钟有效，打死也不能告诉别人"
						, mobile, "0", null);
			}else{
				HttpSender.SendPost(url, account, pswd, content
						, mobile, "0", null);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}

	public static boolean send(final String[] tel, final String content) {

		return true;
	}

	public static boolean send(List<String> tel, String content) {
		return send((String[]) tel.toArray(), content);
	}

}
